package com.ly.friend.controller.user;

import com.ly.common.core.constants.HttpConstants;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.R;
import com.ly.common.core.domain.vo.LoginUserVO;
import com.ly.friend.domain.user.VO.UserVO;
import com.ly.friend.domain.user.dto.UserDTO;
import com.ly.friend.domain.user.dto.UserUpdateDTO;
import com.ly.friend.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    // /user/sendCode
    //要考虑安全问题,毕竟涉及到了手机号,所以选择POST
    @PostMapping("/sendCode")
    //R<Void>,<>中范型的选择,正常是认为会返回验证码-String
    //但是验证码不是返回给前端的,是返回给手机页面上的

    public R<Void> sendCode(@RequestBody UserDTO userDTO) {
        System.out.println();
        return toR(userService.sendCode(userDTO)) ;
    }

    /**
     * 验证验证码  //@NotBlank注解用于验证字符串字段不仅不为null
     * 而且至少包含一个非空白字符
     */

//     /user/code/login
    @PostMapping("/code/login")
    public R<String> codeLogin(@RequestBody UserDTO userDTO){
        return R.ok(userService.codeLogin(userDTO.getPhone(),userDTO.getCode()));
    }

    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION)String token){
        return toR(userService.logout(token));
    }

    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return userService.info(token);
    }
    @GetMapping("/detail")
    public R<UserVO> detail(){
        return  R.ok(userService.detail());
    }

    @PutMapping("/edit")
    public R<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO){
        return toR(userService.edit(userUpdateDTO));
    }

    @PutMapping("/head-image/update")
    public R<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO){
        return toR(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }
}
